package com.hgm.study.ui.components

import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.google.accompanist.placeholder.PlaceholderHighlight
import com.google.accompanist.placeholder.material.placeholder
import com.google.accompanist.placeholder.material.shimmer
import com.google.accompanist.placeholder.placeholder
import com.google.accompanist.placeholder.shimmer
import com.hgm.study.viewmodel.MainViewModel
import kotlinx.coroutines.launch
import java.sql.Time
import java.util.Timer
import java.util.TimerTask

/**
 * @author    HGM
 * @version   1.0
 */
@OptIn(ExperimentalPagerApi::class)
@Composable
fun SwiperContent(vm: MainViewModel) {
      //虚拟页数
      val virtualCount = Int.MAX_VALUE
      //实际页数
      val actualCount = vm.swipers.size
      //初始下标
      val initialIndex = virtualCount / 2
      //轮播图状态
      val pagerState = rememberPagerState(initialPage = initialIndex)
      //协程作用域
      val coroutineScope = rememberCoroutineScope()

      //开启定时器，自动轮播
      DisposableEffect(Unit) {
            coroutineScope.launch {
                  vm.swiperData()
            }
            val timer = Timer()

            timer.schedule(object : TimerTask() {
                  override fun run() {
                        coroutineScope.launch { pagerState.animateScrollToPage(pagerState.currentPage + 1) }
                  }

            }, 3000, 3000)

            onDispose {
                  timer.cancel()
            }
      }

      HorizontalPager(
            count = virtualCount,
            itemSpacing = 16.dp,
            state = pagerState,
            modifier = Modifier
                  .padding(16.dp)
                  .clip(RoundedCornerShape(8.dp)),
      ) { index ->
            val actualIndex = (index - initialIndex).floorMod(actualCount)

            AsyncImage(
                  model = vm.swipers[actualIndex].imageUrl,
                  contentDescription = null,
                  modifier = Modifier
                        .fillMaxWidth()
                        .aspectRatio(7 / 3f)
                        .placeholder(visible = !vm.swiperLoader, highlight = PlaceholderHighlight.shimmer()),
                  contentScale = ContentScale.Crop
            )
      }
}

//扩展
private fun Int.floorMod(other: Int): Int = when (other) {
      0 -> this
      else -> this - floorDiv(other) * other
      //将虚拟数据按照实际数据总数分为 N 组
      //当前虚拟下标是在这虚拟数据中的哪一组：虚拟下标floorDiv实际数据总数（虚拟下标/实际数据总数）。向下取整
      //虚拟下标 -（虚拟下标/实际数据总数）* 实际数据总数
}